package com.joker.controller;

import com.joker.entity.ChatSession;
import com.joker.entity.SessionAndUser;
import com.joker.service.ChatService;
import com.joker.service.ChatSessionService;
import com.joker.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 会话表 前端控制器
 * </p>
 *
 * @author joker
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/chatSession")
@Api(value="ChatSessionController",tags={"用户会话操作接口"})
public class ChatSessionHandler {

    @Autowired
    private ChatSessionService chatSessionService;

    @Autowired
    private ChatService chatService;


    /**
     * 已建立会话列表
     */
    @ApiOperation("查询该用户已建立连接的记录表")
    @GetMapping("/sessionList/{userId}")
    public ResultUtil sessionList(@PathVariable("userId")String userId){
        return ResultUtil.success(chatSessionService.findAllByUserId(userId));
    }
    /**
     * 与对方用户建立会话
     */
    @ApiOperation("建立双方用户的连接存入连接记录表")
    @PostMapping("/creatSession/{userId}/{toUserId}")
    public ResultUtil creatSession(@PathVariable("userId")String userId,@PathVariable("toUserId")String toUserId){
        //判断是否在用户表中存在用户信息，存在才建立连接
        ChatSession tempChatSession = chatSessionService.findByUserIdAndToUserId(userId,toUserId);
        if (tempChatSession == null){
            //未有会话连接记录
            ChatSession chatSession = new ChatSession();
            chatSession.setUserId(userId);
            chatSession.setToUserId(toUserId);
            chatSession.setUnReadCount(0);
            Integer TorF = chatSessionService.save(chatSession);
            if(TorF == 1){
                //返回为1即代表成功插入一条数据
                return ResultUtil.success(chatSessionService.findByUserIdAndToUserId(userId,toUserId),"成功建立会话");
            }else {
                return ResultUtil.error("建立会话异常");
            }
        }else {//若存在会话连接记录，直接返回
            return ResultUtil.success(tempChatSession,"已存在连接记录");
        }
    }
    /**
     * 删除会话
     */
    @ApiOperation("删除该用户已建立连接记录")
    @DeleteMapping("/delSession/{sessionId}")
    public ResultUtil deleteById(@PathVariable("sessionId") Integer sessionId){
        ChatSession chatSession = chatSessionService.findBySessionId(sessionId);
        Integer TorF = chatSessionService.deleteById(sessionId);
        //返回为1即代表成功删除一条数据
        if(TorF == 1){
            String userId = chatSession.getUserId();
            String toUserId = chatSession.getToUserId();
            //当两个人的连接都不存在记录时，清空所有聊天记录
            if(chatSessionService.findByUserIdAndToUserId(userId,toUserId) == null &&
                    chatSessionService.findByUserIdAndToUserId(toUserId,userId) == null){
                chatService.delete(userId,toUserId);
                chatService.delete(toUserId,userId);
            }
            return ResultUtil.success(null,"成功删除会话");
        }else {
            return ResultUtil.error("删除会话异常");
        }
    }

}
